package com.lanou.springbootoracle.service.impl;

import com.lanou.springbootoracle.mapper.DeptMapper;
import com.lanou.springbootoracle.pojo.Dept;
import com.lanou.springbootoracle.service.DeptService;
import com.lanou.springbootoracle.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public List<Dept> findAll() {
        List<Dept> list = null;
        Object obj = redisUtil.get("list");
        if(obj ==null){
            list = deptMapper.findAll();
            redisUtil.set("list",list);
        }else {
            list = (List<Dept>) obj;
        }
        return list;
    }

    @Override
    public Dept findById(Integer deptno) {
        Dept dept = null;
        Object obj = redisUtil.get("dept:"+deptno);
        if(obj ==null){
            dept = deptMapper.findById(deptno);
            redisUtil.set("dept:"+deptno,dept);
        }else {
            dept = (Dept)obj;
        }
        return dept;
    }

    @Override
    public int save(Dept dept) {
        return deptMapper.save(dept);
    }

    @Override
    public int delete(Integer deptno) {
        return deptMapper.delete(deptno);
    }

    @Override
    public int update(Dept dept) {
        return deptMapper.update(dept);
    }
}
